package com.lp.controller;

import com.lp.po.PagingVO;
import com.lp.po.Scenery;
import com.lp.po.SceneryList;
import com.lp.service.ArgumentService;
import com.lp.service.SceneryListService;
import com.lp.service.SceneryService;
import com.lp.userException.CustomException;
import com.lp.utils.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admin/Scenery")
public class AdminSceneryController {
    @Resource
    private SceneryService sceneryService;
    @Resource
    private SceneryListService sceneryListService;
    @Resource
    private ArgumentService argumentService;


    /***************************景点管理设置(图片两组上传、上传多份为实现)****************************************/
    @RequestMapping(value = "/editScenery",method = RequestMethod.GET)
    public String editScenery(Model model,Integer id) throws Exception {
        if (id == null) {
            return "redirect:/admin/showScenery";
        }
        Scenery scenery = sceneryService.findById(id);
        if (scenery == null) {
            throw new CustomException("没有该景点");
        }
        List<SceneryList> sceneryLists=sceneryListService.findAll();
        model.addAttribute("scenery", scenery);
        model.addAttribute("sceneryLists", sceneryLists);
        return "/admin/editScenery";
    }
    @RequestMapping(value = "/doEditScenery",method = RequestMethod.POST)
    public String doEditScenery(MultipartFile file,Scenery scenery) throws Exception {
        if(!file.isEmpty()){
            String uploadUrl = argumentService.getByName("uploadUrl").getValue();
            scenery.setTximg(ImageUtil.getNewFileName(file,uploadUrl));
        }
        sceneryService.updataSceneryWhitId(scenery.getId(),scenery.getSname(),scenery.getLatitude(),
                    scenery.getLongitude(),scenery.getWidth(),scenery.getTximg(),scenery.getFloor(),
                    scenery.getDescription(),scenery.getScenerylistid());
        return "redirect:/admin/Scenery/showScenery";
    }
    @RequestMapping("/showScenery")
    public String showScenery(Model model, Integer page) throws Exception {
        List<Scenery> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int sceneryNumber=sceneryService.getCount();
        //设置总页数
        pagingVO.setTotalCount(this.sceneryService.getCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = sceneryService.findAllScenery(1);
        } else {
            pagingVO.setToPageNo(page);
            list = sceneryService.findAllScenery(page);
        }
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("sceneryList",list);
        model.addAttribute("num", sceneryNumber);
        return "/admin/showScenery";
    }
    @RequestMapping(value = "/removeScenery",method = RequestMethod.GET)
    public String removeScenery(Integer id) throws Exception {
        sceneryService.delectScenery(id);
        return "redirect:/admin/Scenery/showScenery";
    }
    @RequestMapping(value = "/selectScenery", method = {RequestMethod.POST})
    private String selectScenery(String sname, Model model, Integer page) throws Exception {
        PagingVO pagingVO = new PagingVO();
        List<Scenery> list=null;
        int sceneryNumberByName=0;
        pagingVO.setTotalCount(this.sceneryService.getCountByName('%'+sname+'%'));
        if(sname!=null){
            if (page == null || page == 0) {
                pagingVO.setToPageNo(1);
                list = sceneryService.findBySceneryName(1,'%'+sname+'%');
                sceneryNumberByName=sceneryService.getCountByName('%'+sname+'%');
            } else {
                pagingVO.setToPageNo(page);
                list = sceneryService.findBySceneryName(page,'%'+sname+'%');
                sceneryNumberByName=sceneryService.getCountByName('%'+sname+'%');
            }
            model.addAttribute("pagingVO", pagingVO);
            model.addAttribute("sceneryList", list);
            model.addAttribute("num", sceneryNumberByName);
            return "/admin/showScenery";
        }else{
            return "/admin/showScenery";
        }
    }
    @RequestMapping(value = "/addScenery")
    public String addScenery(Model model) throws Exception {
        List<SceneryList> list = sceneryListService.findAll();
        model.addAttribute("sceneryList", list);
        return "admin/addScenery";
    }
    @RequestMapping(value = "/doAddScenery",method = RequestMethod.POST)
    public String doAddScenery(Model model, Scenery scenery, MultipartFile file) throws Exception {
        String uploadUrl = argumentService.getByName("uploadUrl").getValue();
        //图片上传成功后，将图片的地址写到数据库
        String filePath =uploadUrl +"xiaoyuan\\image";//保存图片的路径
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        //新的文件名字
        String newFileName = UUID.randomUUID()+originalFilename;
        //封装上传文件位置的全路径
        File targetFile = new File(filePath,newFileName);
        //File wxtargetFile = new File(wxfilePath,newFileName);
        //把本地文件上传到封装上传文件位置的全路径
        file.transferTo(targetFile);
        //file.transferTo(wxtargetFile);

        scenery.setTximg(newFileName);

        Boolean result = sceneryService.save(scenery);
        if (!result) {
            model.addAttribute("message", "景点名称重复");
            return "forward:/admin/Scenery/addScenery";
        }
        return "redirect:/admin/Scenery/showScenery";
    }
}
